const request = require('request-promise').defaults({ json: true, gzip: true })
const config = require('../config');

const apiRoot = config.apiRoot
const ServerQQ = config.ServerQQ
function callFunc (name, params) {
  const url = `${apiRoot}/v1/LuaApiCaller?qq=${ServerQQ}&funcname=${name}&timeout=10`
  if (params) return request.post(url, { body: params })
  return request.get(url)
}
module.exports = {
  callFunc,
  request:(url,type='get',params)=>{
    return type==='get'?request.get(apiRoot+url):request.post(apiRoot+url,{body:params})
  },
  get:(url,withoutBase=false)=>{
    return withoutBase? request.get(url) : request.get(apiRoot+url)
  },
  post:(url,params,withoutBase=false)=>{
    return withoutBase?request.post(url,{body:params}): request.post(apiRoot+url,{body:params})
  }
}